package com.vay.de.dang.online.util;

import android.content.Intent;
import android.widget.TextView;

import java.util.List;

import com.vay.de.dang.online.resp.RCodeTableDto;
import com.vay.de.dang.online.ui.BaseActivity;
import com.vay.de.dang.online.ui.bill.RepaymentActivity;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.entiy.CommodityEntity;
import com.vay.de.dang.online.entiy.MenuItemEntity;
import com.vay.de.dang.online.resp.RCustomerDataDto;
import com.vay.de.dang.online.resp.REmergencyContactDto;
import com.vay.de.dang.online.web.BaseApi;

public class ResponseHelper {

    public static List<REmergencyContactDto> emergencyContact;
    public static RCustomerDataDto customerData;

    public static String filePreview;

    public static void setEmergencyContact(List<REmergencyContactDto> list) {
        emergencyContact = list;
    }

    public static void setCustomerData(RCustomerDataDto bean) {
        customerData = bean;
    }

    public static MenuItemEntity toOptionItem(RCodeTableDto dto) {
        MenuItemEntity bean = new MenuItemEntity();
        bean.setLabel(dto.getLabel());
        bean.setValue(dto.getValue());
        bean.setCurrentCodeType(dto.getCurrentCodeType());
        bean.setParentCodeKey(dto.getParentCodeKey());
        bean.setParentCodeType(dto.getParentCodeType());
        return bean;
    }

    public static void setFilePreview(String path) {
        filePreview = path;
    }

    public static String buildPreview(String path) {
        return !VnUtil.isEmpty(filePreview) && !VnUtil.isEmpty(path) ? filePreview + path : null;
    }

    public static void applyButton(BaseActivity activity,
                                   CommodityEntity entity, TextView bnt) {
        int state = entity.getState();
        // 状态：0 申请 1 拒贷期 2 再次申请 3 去还款 4 审核中 5 待取款
        // 申请
        if (state == 0) {
            bnt.setText(R.string.bnt_apply);
            bnt.setOnClickListener(view -> {
                // 业务埋点
                BaseApi.execBuriedPoint(2020, 2000,
                        "主页", "申请", entity.getCommodityNum());
                // 申请
                RequestHelper.clickApply(activity, entity);
            });
        }
        // 拒绝
        else if (state == 1) {
            bnt.setText(R.string.bnt_apply_refuse);
            bnt.setOnClickListener(view -> {
                activity.showHint(R.string.label_apply_refuse);
            });
        }
        // 再次申请
        else if (state == 2) {
            bnt.setText(R.string.bnt_apply_again);
            bnt.setOnClickListener(view -> {
                // 业务埋点
                BaseApi.execBuriedPoint(2040, 2000, "主页",
                        "再次申请", entity.getCommodityNum());
                // 再次申请
                RequestHelper.clickApply(activity, entity);
            });
        }
        // 去还款
        else if (state == 3) {
            bnt.setText(R.string.bnt_apply_to_repay);
            bnt.setOnClickListener(view -> {

                // 业务埋点
                BaseApi.execBuriedPoint(2050, 2000, "主页",
                        "去还款", entity.getCommodityNum());

                toRepayment(activity, entity.getApplyNo());
            });
        }
        // 审核中
        else if (state == 4) {
            bnt.setText(R.string.bnt_apply_in_review);
            bnt.setOnClickListener(null);
            bnt.setBackgroundResource(R.drawable.bg_bnt_apply_audit);
        }
    }

    // 去还款
    public static void toRepayment(BaseActivity activity, String applyNo) {
        Intent intent = new Intent();
        intent.setClass(activity, RepaymentActivity.class);
        intent.putExtra("applyNo", applyNo);
        activity.startActivity(intent);
    }
}
